//
//  SouSuoXQController.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/2/18.
//  Copyright © 2016年 game. All rights reserved.
//

#import "SouSuoXQController.h"
#import "SyntSearchTableview.h"
#import "searchIslanTablview.h"
#import "IslanDescribCtll.h"
#import "SearchArticleTableview.h"
#import "SearchSelectTableview.h"
#import "VideoDescrib.h"
#import "NewestAticleCtr.h"
#define btnOfScrWith ((SCREEN_WIDTH-60)/5)  //滑动btn的宽度

@interface SouSuoXQController ()<UIScrollViewDelegate,UITextFieldDelegate,searchIslanTablviewClickDeleget,SearchArticleTableviewDeleget,SyntSearchTableviewDeleget>
@property (weak, nonatomic) IBOutlet UITextField *TextField;
///**
// *  承载按钮的scrollview
// */
@property(nonatomic,strong)UIScrollView *BtnscrollView;
//
///**
// *  承载Tableview的UIScrollView
// */
@property(nonatomic,strong)UIScrollView *TBscrollView;
//
///**
// *  btn 上的滑条
// */
@property (nonatomic,strong) UIView *slidView;
@property (nonatomic,strong) NSArray *titleAry;
//
@property (nonatomic,strong) SyntSearchTableview *sysTableview;

@property (nonatomic,strong)  searchIslanTablview *diaochangTab;
@property (nonatomic,strong)  searchIslanTablview *dianpuTab;
@property (nonatomic,strong)  SearchArticleTableview *seachTab;
@property (nonatomic,strong)  SearchArticleTableview *VideoseachTab;
@property (nonatomic,strong)  SearchSelectTableview * selectTab;
@end

@implementation SouSuoXQController

-(NSArray *)titleAry{
    if (!_titleAry) {
        _titleAry = @[@"综合",@"文章",@"视频",@"论坛",@"钓场",@"渔具店"];
    }
    return _titleAry;
}

-(UIView *)slidView{
    if (!_slidView) {
        _slidView = [[UIView alloc]initWithFrame:CGRectMake(0, H(self.BtnscrollView)-1, btnOfScrWith, 1)];
        _slidView.backgroundColor  = mainColor;
        [self.BtnscrollView addSubview:_slidView];
    }
    return _slidView;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.app.myTabBar.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    StatusBlack;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.TextField.delegate=self;
    self.TextField.leftView = [self textFileLweftView];
    self.TextField.leftViewMode=UITextFieldViewModeAlways;
    [self CreatScrollViewAndBtn];
    [self CreatTBScrollView];
}

-(void)creatTableView{
    
        for (int i=0; i<self.titleAry.count; i++) {
            NSString *title = self.titleAry[i];
        if ([title isEqualToString:@"综合"]) {
        
            self.sysTableview = [[SyntSearchTableview alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*i,0, SCREEN_WIDTH,SCREEN_HEIGHT-110) style:UITableViewStylePlain];
            self.sysTableview.searchString = _searchString;//引起内存上升
            self.sysTableview.fatherCtrl =self;
            self.sysTableview.mainDelget = self;
            [self.TBscrollView addSubview:self.sysTableview];
            
        }
        else
            if ([title isEqualToString:@"钓场"]) {

            searchIslanTablview *diaochang = [[searchIslanTablview alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i,0, SCREEN_WIDTH,SCREEN_HEIGHT-110) style:UITableViewStylePlain];
            diaochang.isYujudian = NO;
            diaochang.url = [NSString stringWithFormat:islanSeachUrl,@"diaochang"];
            diaochang.Maindelget = self;
            diaochang.seachString = _searchString;
             [self.TBscrollView addSubview:diaochang];
            self.diaochangTab = diaochang;
        
        }else if ([title isEqualToString:@"渔具店"]) {

            searchIslanTablview *dianpu = [[searchIslanTablview alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i,0, SCREEN_WIDTH,SCREEN_HEIGHT-110) style:UITableViewStylePlain];
            dianpu.isYujudian = YES;
            dianpu.url = [NSString stringWithFormat:islanSeachUrl,@"dianpu"];
            dianpu.seachString = _searchString;
            dianpu.Maindelget = self;
            [self.TBscrollView addSubview:dianpu];
            self.dianpuTab = dianpu;
            
        }
        
        else if ([title isEqualToString:@"文章"]){
            
            SearchArticleTableview *seach = [[SearchArticleTableview alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i,0, SCREEN_WIDTH,SCREEN_HEIGHT-110) style:UITableViewStylePlain];
            seach.seachDic = @{@"keyword":_searchString};
            seach.mainDelget = self;
            [self.TBscrollView addSubview:seach];

            self.seachTab = seach;
        }else if ([title isEqualToString:@"视频"]){

            SearchArticleTableview *seach = [[SearchArticleTableview alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i,0, SCREEN_WIDTH,SCREEN_HEIGHT-110) style:UITableViewStylePlain];
            seach.seachDic = @{@"keyword":_searchString,@"nid":@"video"};
            seach.mainDelget = self;
            [self.TBscrollView addSubview:seach];
            self.VideoseachTab = seach;
        }else if([title isEqualToString:@"论坛"]){
            
            SearchSelectTableview * tableview=[[SearchSelectTableview alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*i,0, SCREEN_WIDTH,SCREEN_HEIGHT-110) style:UITableViewStylePlain];
            tableview.fatherCtrl = self;
            tableview.searchString = _searchString ;
            [self.TBscrollView addSubview:tableview];
            self.selectTab = tableview;
            
        }
       
    }
}

-(void)FootTapClickWhithIndex:(NSInteger)index{
    
    [self.TBscrollView setContentOffset:CGPointMake(SCREEN_WIDTH*index, 0) animated:YES];
    
    for (int i=0; i<self.titleAry.count; i++) {
        
        UIButton *Btn=(UIButton*)[self.view viewWithTag:10+i];
        if (index==Btn.tag-10) {
            [Btn setTitleColor:mainColor forState:UIControlStateNormal];
            //让上方承载btn的BtnscrollView跟着滚动;
            
            [UIView animateWithDuration:0.3 animations:^{
                self.slidView.x = Btn.x;
                [self ContSetWhithIndex:Btn.tag-10];
            }];
            
        }else{
            [Btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
}

-(void)setSearchString:(NSString *)searchString{
    _searchString = searchString;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.TextField.text = _searchString;
    });
}

-(void)IsLanTabClickWhithDetail:(NSString *)detail AndLatitude:(NSString *)latitude AndLongitude:(NSString *)longitude AndLocaltion:(NSString *)localtion AndID:(NSString *)Id AndisYujudian:(BOOL)isyujudian{

    IslanDescribCtll *desCreib = [IslanDescribCtll new];
    desCreib.isYuJuDian = isyujudian;
    desCreib.detail = detail;
    desCreib.latitude = latitude;
    desCreib.longitude = longitude;
    desCreib.localtion = localtion;
    desCreib.ID = Id;
    
    [self.navigationController pushViewController:desCreib animated:YES];
}

-(void)ArticleClickWhithId:(NSString *)Id AndSkin:(NSInteger)skin{


    if (skin == 1){
        VideoDescrib *video = [VideoDescrib new];
        video.AticleID = Id;
        [self.navigationController  pushViewController:video animated:YES];
    }else{
        NewestAticleCtr *aticle = [NewestAticleCtr new];
        aticle.AticleID = Id;
        [self.navigationController pushViewController:aticle animated:YES];
    }

}

/**
 *  textFile左边视图  放大镜
 */
-(UIView *)textFileLweftView{
    UIView *bacView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, H(self.TextField), H(self.TextField))];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btn-search-grey"]];
    imageView.frame = CGRectMake(5,5,W(bacView)-10,W(bacView)-10);
    [bacView addSubview:imageView];
    return bacView;
}

/**
 * textField Deleget  点击键盘右下角搜索  进行的action
 *
 */
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if ([string isEqualToString:@"\n"]) {
        [self SearChiWhithString:textField.text];
    }
    return YES;
}
-(void)SearChiWhithString:(NSString *)searchStr{

    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
    
    self.sysTableview.searchString = searchStr;
    self.diaochangTab.seachString = searchStr;
    self.dianpuTab.seachString = searchStr;
    self.seachTab.seachDic =  @{@"keyword":searchStr};
     self.VideoseachTab.seachDic = @{@"keyword":searchStr,@"nid":@"video"};
    self.selectTab.searchString = searchStr;
}

- (IBAction)CanctionAction:(UIButton *)sender {
    
    self.sysTableview.fatherCtrl = nil;
    self.selectTab.fatherCtrl = nil;
    
    self.diaochangTab = nil;
    self.dianpuTab = nil;
    self.seachTab = nil;
    self.VideoseachTab = nil;
    self.selectTab = nil;
    self.BtnscrollView = nil;
    self.TBscrollView = nil;
    
    [self.navigationController popViewControllerAnimated:YES];
}


//#pragma mark - 承载按钮的scrollview
-(void)CreatScrollViewAndBtn
{
    self.BtnscrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 65, SCREEN_WIDTH, 44)];
    [self.view addSubview:self.BtnscrollView];
    self.BtnscrollView.contentSize=CGSizeMake(SCREEN_WIDTH/5*self.titleAry.count, 0);
    self.BtnscrollView.showsHorizontalScrollIndicator=NO;
    self.BtnscrollView.backgroundColor = [UIColor whiteColor];
    
    [self Linview:CGRectMake(0, H(self.BtnscrollView)-0.5, self.BtnscrollView.contentSize.width, 0.5) BackgroundColor:linColor AddView:self.BtnscrollView];
    [self CreatBtn];

}
-(void)CreatBtn{
    
    for (int i=0; i<self.titleAry.count; i++) {
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame=CGRectMake((btnOfScrWith+10)*i+10,10, btnOfScrWith, 24);
        [btn setTitle:self.titleAry[i] forState:UIControlStateNormal];
        [self.BtnscrollView addSubview:btn];


        btn.tag=10+i;
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        if (i==0) {
            [btn setTitleColor:mainColor forState:UIControlStateNormal];
            
            self.slidView.x = btn.x;
        }
     }
    
}

///**
// *  点击或滑动时  让 BtnscrollView 跟着走
// */
-(void)ContSetWhithIndex:(NSInteger )index{

    NSInteger endIndex = self.titleAry.count%5;
    if (index>=1) {
        [self.BtnscrollView setContentOffset:CGPointMake((btnOfScrWith+10)*(index-1), 0) animated:YES];
        
        if (index>endIndex) {//到了最后一页BtnscrollView就不在往前移动
            [self.BtnscrollView setContentOffset:CGPointMake((btnOfScrWith+10)*endIndex, 0) animated:NO];
        }
    }
}

-(void)btnAction:(UIButton*)btn
{
    [UIView animateWithDuration:0.3 animations:^{
         self.slidView.x = btn.x;
        [self ContSetWhithIndex:btn.tag-10];
    }];
   
    
    [self.TBscrollView setContentOffset:CGPointMake(SCREEN_WIDTH*(btn.tag-10), 0) animated:YES];
    for (int i=0; i<self.titleAry.count; i++) {
        UIButton *Btn=(UIButton*)[self.view viewWithTag:10+i];
        if (Btn.tag==btn.tag) {
            [Btn setTitleColor:mainColor forState:UIControlStateNormal];
        }else{
            [Btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
}

//#pragma mark - 承载Tableview的UIScrollView
-(void)CreatTBScrollView
{
    
    self.TBscrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0,110, SCREEN_WIDTH,SCREEN_HEIGHT-110)];
    [self.view addSubview:self.TBscrollView];
    self.TBscrollView.delegate=self;
//    self.TBscrollView.backgroundColor=[UIColor lightGrayColor];
    self.TBscrollView.contentSize=CGSizeMake(SCREEN_WIDTH*self.titleAry.count, 0);
    self.TBscrollView.showsHorizontalScrollIndicator=NO;
    self.TBscrollView.pagingEnabled=YES;
    self.TBscrollView.tag=10000;
    self.TBscrollView.bounces = NO;
    
    [self creatTableView];
    
}

#pragma mark - scrollview代理方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.tag==10000) {
        for (int i=0; i<self.titleAry.count; i++) {
            UIButton *Btn=(UIButton*)[self.view viewWithTag:10+i];
            if (scrollView.contentOffset.x/SCREEN_WIDTH==Btn.tag-10) {
                [Btn setTitleColor:mainColor forState:UIControlStateNormal];
             //让上方承载btn的BtnscrollView跟着滚动;

                [UIView animateWithDuration:0.3 animations:^{
                    self.slidView.x = Btn.x;
                    [self ContSetWhithIndex:Btn.tag-10];
                }];

            }else{
                [Btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            }
        }
    }
}
//
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear: animated];
//    self = nil;
    
    
}


-(void)dealloc{
//    NSLog(@"2222销毁");
    
}

@end
